﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;

using System.Collections.Generic;
using QueryBase;
using System.Web.Configuration;
using System.Data.Linq.SqlClient;

namespace QueryCommon
{
    [ServiceContract(Namespace = "QueryCommon")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class QueryWCF
    {
        CoastripDataContext DC = new CoastripDataContext(WebConfigurationManager.ConnectionStrings["CoastripConnectionString"].ConnectionString);

        public Dictionary<int, short> GetVoyageWharfDicByVnx(int Vnx)
        {
            var Res1 = from y in DC.Wharfiny
                       join f in DC.Wharfrom on y.Fnx equals f.Fnx
                       where (f.Vnx == Vnx)
                       select new
                       {
                           FromW = f.Wmx
                       };
            //按数量排序，靠后的码头在前，靠前的码头在后
            var Res2 = from r1 in Res1
                       group r1 by new
                       {
                           r1.FromW
                       }
                           into zz
                           orderby zz.Count()
                           select new
                           {
                               Key = zz.Key,
                               CountW = zz.Count()
                           };
            //按数量倒序排列，第一行即是终点码头
            var Res3 = from y in DC.Wharfiny
                       join f in DC.Wharfrom on y.Fnx equals f.Fnx
                       where (f.Vnx == Vnx)
                       select new
                       {
                           DestW = y.Wmx
                       };
            var Res4 = from r3 in Res3
                       group r3 by new
                       {
                           r3.DestW
                       }
                           into zzz
                           orderby zzz.Count() descending
                           select new
                           {
                               Key = zzz.Key,
                               CountW = zzz.Count()
                           };

            Dictionary<int, short> dWharf = new Dictionary<int, short>();
            //先添加终点
            dWharf.Add(0, Res4.First().Key.DestW);
            foreach (var c in Res2)
            {
                dWharf.Add(c.CountW, c.Key.FromW);
            }
            return dWharf;
        }

        private int VoyageRestCount(short Gmx, int Ynx, int Vnx)
        {
            int iFlag = DC.Gfare.Where(y => y.Gmx == Gmx && y.Ynx == Ynx).First().Flag;
            var Result = from b in DC.Vbunk
                         join c in DC.Cabin on b.Cmx equals c.Cmx
                         where (c.Gmx == Gmx)
                         && (b.Vnx == Vnx)
                         select new
                         {
                             Mask = b.Mask
                         };
            return Result.Count(y => (y.Mask & iFlag) == 0);
        }

        public bool GetVoyageRest(int Vnx)
        {
            var Result = from f in DC.Wharfiny
                         join o in DC.Wharfrom on f.Fnx equals o.Fnx
                         join wf in DC.Wharf on f.Wmx equals wf.Wmx
                         join wo in DC.Wharf on o.Wmx equals wo.Wmx
                         join v in DC.Voyage on o.Vnx equals v.Vnx
                         join l in DC.Liner on v.Lmx equals l.Lmx
                         join y in DC.Yacht on l.Ymx equals y.Ymx
                         join g in DC.Grade on y.Ymx equals g.Ymx
                         where (v.Vnx == Vnx)
                         orderby g.Gmx, wo.Wmx, wf.Wmx
                         select new
                         {
                             Gmx = g.Gmx,
                             Ynx = f.Ynx,
                             Vnx = v.Vnx,
                             GName = g.Nym,
                             FromW = wo.Nym,
                             DestW = wf.Nym,
                         };

            bool bResult = false;
            foreach (var c in Result)
            {
                if (VoyageRestCount(c.Gmx, c.Ynx, c.Vnx) > 0) bResult = true;
            }
            return bResult;
        }

        [OperationContract]
        public List<VoyageClass> GetVoyageCommon(int PageNum)
        {
            var ResVoy = from v in DC.Voyage
                         join o in DC.Wharfrom on v.Vnx equals o.Vnx
                         join l in DC.Liner on v.Lmx equals l.Lmx
                         join y in DC.Yacht on l.Ymx equals y.Ymx
                         join c in DC.Course on l.Cmx equals c.Cmx
                         where (SqlMethods.DateDiffDay(o.Leave, DateTime.Now) == 0)
                         select new
                         {
                             Vnx = v.Vnx,
                             Wmx = o.Wmx,
                             CName = c.Nym,
                             YName = y.Nym,
                             Sail = v.Sail,
                             Sell = v.Sell,
                             Leave = o.Leave
                         };

            List<VoyageClass> TempList = new List<VoyageClass>();
            foreach (var f in ResVoy)
            {
                Dictionary<int, short> dWharf = GetVoyageWharfDicByVnx(f.Vnx);
                if (f.Wmx == dWharf[dWharf.Count - 1])
                {
                    VoyageClass ccc = new VoyageClass
                    {
                        CName = DC.Wharf.Where(y => y.Wmx == dWharf[dWharf.Count - 1]).First().Nym + "-->" +
                                DC.Wharf.Where(y => y.Wmx == dWharf[0]).First().Nym,
                        YName = f.YName,
                        Sail = f.Sail,
                        Sell = f.Sell,
                        Leave = f.Leave.ToString("MM-dd HH:mm"),
                        Rest = GetVoyageRest(f.Vnx)
                    };
                    TempList.Add(ccc);
                }
            }

            //根据请求的页数获得该页的记录集，PageNum等于0为请求末页
            var results = PageNum == 0 ?
                TempList.Skip(Pub.PageRowCount * (TempList.Count() / Pub.PageRowCount)).Take(Pub.PageRowCount) :
                TempList.Skip(Pub.PageRowCount * (PageNum - 1)).Take(Pub.PageRowCount);

            List<VoyageClass> ResultList = new List<VoyageClass>();
            foreach (var c in results)
            {
                VoyageClass ccc = new VoyageClass
                {
                    PageNumber = Pub.GetPageCount(TempList.Count()),
                    CName = c.CName,
                    YName = c.YName,
                    Sail = c.Sail,
                    Sell = c.Sell,
                    Leave = c.Leave,
                    Rest = c.Rest
                };
                ResultList.Add(ccc);
            }
            return ResultList;
        }

    }
}
